package com.lxujia.ruankao.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lxujia.ruankao.R;
import com.lxujia.ruankao.activity.QuestionItemActivity;
import com.lxujia.ruankao.adapter.CategoryListViewAdapter;
import com.lxujia.ruankao.model.CategoryItem;
import com.lxujia.ruankao.model.CategoryType;
import com.lxujia.ruankao.model.QuestionItem;
import com.lxujia.ruankao.util.LogUtil;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class OldExamsFragment extends android.support.v4.app.Fragment {
    private final static String TAG = "OldExamsFragment";
    private String mCategoryType = "pm";
    private String mCategoryName = "信息系统项目管理师";

    private List<CategoryItem> mCategoryItemList;
    private List<Integer> mCountList;

    private View mFragmentView;

    public OldExamsFragment() {}

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mFragmentView = inflater.inflate(R.layout.fragment_category_list, container,false);

        // 由于 categoryType 唯一，查一行就可以了。并查出关联的 CategoryItem
        CategoryType categoryType = LitePal.where("categoryType == ?", mCategoryType).
                findFirst(CategoryType.class);
        if (categoryType == null) {
            Toast.makeText(getActivity(), R.string.db_loading, Toast.LENGTH_LONG).show();
        } else {
            mCategoryName = categoryType.getCategoryName();
        }
        LogUtil.d(TAG, "mCategoryName = " + mCategoryName);
        String linkId = CategoryType.class.getSimpleName().toLowerCase();
        mCategoryItemList = LitePal.where(linkId + "_id == ?",
                String.valueOf(categoryType.getId())).order("period desc").find(CategoryItem.class);
        //initUI(container, inflater);
        mCountList = loadCountList(mCategoryItemList);
        return mFragmentView;
    }

    private List<Integer> loadCountList(List<CategoryItem> mCategoryItemList) {
        List<Integer> countList = new ArrayList<>();
        String linkId = CategoryItem.class.getSimpleName().toLowerCase();
        String conditions = linkId + "_id == ? and userAnswer != -1";
        int undoingCount = 75;
        for (CategoryItem item : mCategoryItemList) {
            undoingCount = LitePal.where(conditions, String.valueOf(item.getId())).count(QuestionItem.class);
            countList.add(undoingCount);
        }
        return countList;
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        TextView nameTx = (TextView) getView().findViewById(R.id.category_name_tv);
        nameTx.setText(mCategoryName);

        ListView listView = (ListView) getView().findViewById(R.id.category_list);
        final CategoryListViewAdapter adapter = new CategoryListViewAdapter(getActivity(), true);
        adapter.setData(mCategoryItemList);
        adapter.setCountData(mCountList);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                final Integer categoryId = mCategoryItemList.get(i).getId();

                Intent intent = new Intent(getActivity(), QuestionItemActivity.class);
                intent.putExtra("categoryId", categoryId);
                intent.putExtra("isError", false);
                startActivity(intent);
            }
        });
        listView.setAdapter(adapter);

        adapter.notifyDataSetChanged();
    }
}
